#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#pragma comment(lib,"Winmm.lib")

#define high 700  // 游戏画面尺寸
#define width 500

// 全局变量
IMAGE img_bk;
IMAGE img_plane1,img_plane2;
IMAGE img_bul1,img_bul2;
IMAGE img_ene1,img_ene2;
IMAGE img_boom1,img_boom2;
int plane_x, plane_y;//飞机位置
int bul_x,bul_y;//子弹位置
int ene_x,ene_y;//敌人位置
int score=0;//分数
int boom_x,boom_y;//爆炸飞机
int bul[high][width];//数组子弹
int i,j;
void startup()  // 数据初始化
{
     //播放背景音乐
	mciSendString("open D:\\game_music.mp3 alias bkmusic",NULL,0,NULL);
    mciSendString("play bkmusic repeat",NULL,0,NULL);

	initgraph(width,high);//画面大小

    //导入图片
	loadimage(&img_bk, "D:\\background.jpg");
	
	loadimage(&img_plane1, "D:\\planeNormal_1.jpg");
	loadimage(&img_plane2, "D:\\planeNormal_2.jpg");

    loadimage(&img_bul1, "D:\\bullet1.jpg");
	loadimage(&img_bul2, "D:\\bullet2.jpg");
    
	loadimage(&img_ene1, "D:\\enemyPlane1.jpg");
	loadimage(&img_ene2, "D:\\enemyPlane2.jpg");
	
    loadimage(&img_boom1, "D:\\planeExplode_1.jpg");
	loadimage(&img_boom2, "D:\\planeExplode_2.jpg");
	//设定初始坐标
    plane_x=200;
	plane_y=500;
	ene_x=(rand()%3)*100;
	ene_y=(rand()%2)*100;
    bul_x=800;
	bul_y=800;
    boom_x=500;
	boom_y=700;

	BeginBatchDraw();
}

void clean()  // 显示画面
{
	

}

void show()  // 显示画面
{
	putimage(0, 0, &img_bk);
    //飞机
	putimage(plane_x, plane_y,&img_plane1,NOTSRCERASE);
	putimage(plane_x, plane_y, &img_plane2,SRCINVERT);
    //敌人
    putimage(ene_x, ene_y, &img_ene1, NOTSRCERASE);
    putimage(ene_x, ene_y, &img_ene2,SRCINVERT);

    //爆炸
    putimage(boom_x, boom_y, &img_boom1, NOTSRCERASE);
	putimage(boom_x, boom_y, &img_boom2,SRCINVERT);
    
	//子弹
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(bul[i][j]==1)
			{
				
                putimage(j, i, &img_bul1, NOTSRCERASE);
	            putimage(j, i, &img_bul2,SRCINVERT);
               
			}
		}
	}

	outtextxy(200,600,"得分为:");
	
	FlushBatchDraw();
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	//敌机下落
	if(ene_y<700)
	{
    	static int speed=5; 
    	if(speed<5)
	        speed++;
    	if(speed==5)
		{
	    	ene_y++;
	    	speed=0;
		}
	}
    
	//敌机到底
	if(ene_y==700)
	{
		score--;
		ene_x=(rand()%5)*100;
		ene_y=0;
	}
	
	//子弹上升
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(bul[i][j]==1)
			{
				bul[i-1][j]=1;
				bul[i][j]=0;
			}
		}
	}
	
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(bul[i][j]==1)
			{
			    if(i==ene_y+150)
				{
		            if(j>ene_x&&j<ene_x+148)
					{
		            	score++;
                        ene_x=(rand()%5)*100;
	            	    ene_y=0;
			
	            		mciSendString("close hitmusic ",NULL,0,NULL);
	            		mciSendString("open D:\\gotEnemy.mp3 alias hitmusic ",NULL,0,NULL);
		            	mciSendString("play hitmusic ",NULL,0,NULL);	
					}
				}
			}
		}
	}	
	//子弹击中
	if(bul_y==ene_y+150)
	{
		if(bul_x>ene_x&&bul_x<ene_x+148)
		{
			score++;
            ene_x=(rand()%5)*100;
		    ene_y=0;
			
			mciSendString("close hitmusic ",NULL,0,NULL);
			mciSendString("open D:\\gotEnemy.mp3 alias hitmusic ",NULL,0,NULL);
			mciSendString("play hitmusic ",NULL,0,NULL);	
		}
	}

	
	//飞机相撞
	if(plane_x+50>=ene_x&&plane_x<=ene_x+50)
	{
		if(plane_y<ene_y+50&&plane_y+50>ene_y)
		{
			boom_x=plane_x;
			boom_y=plane_y;
            mciSendString("close boommusic ",NULL,0,NULL);
			mciSendString("open D:\\explode.mp3 alias boommusic ",NULL,0,NULL);
			mciSendString("play boommusic ",NULL,0,NULL);


			Sleep(700);
			exit(0);
		
		}
	}

}

void updateWithInput()  // 与用户输入有关的更新
{
	MOUSEMSG m;//记录鼠标信息
	while(MouseHit())
	{
		m=GetMouseMsg();

		if(m.uMsg==WM_MOUSEMOVE)//鼠标位置=飞机位置
		{
			plane_x=m.x-50;
			plane_y=m.y-50;
		}
		else if(m.uMsg==WM_LBUTTONDOWN)//左键发射子弹
		{
			bul[plane_y-20][plane_x+43]=1;
			mciSendString("close shootmusic ",NULL,0,NULL);
			mciSendString("open D:\\f_gun.mp3 alias shootmusic ",NULL,0,NULL);
			mciSendString("play shootmusic ",NULL,0,NULL);
		}
	}
    /*char input;
    
	if(kbhit())     //当按键时执行 
	{
		input= getch();
		//飞机控制变化 
		if(input=='a')
		{ 
		    if(plane_x>0)
				plane_x-=5;
		}
		if(input=='d')
		{ 
		    if(plane_x+117<width)
				plane_x+=5;
		} 
		if(input=='w')
		{ 
		    if(plane_y>0)
				plane_y-=5;
		}
		if(input=='s')
		{ 
		    if(plane_y+117<high)
				plane_y+=5;
		}
		//子弹输入变化
		if(input==' ')
		{ 
			bul_x=plane_x+43;
			bul_y=plane_y-20;
			mciSendString("close shootmusic ",NULL,0,NULL);
			mciSendString("open D:\\f_gun.mp3 alias shootmusic ",NULL,0,NULL);
			mciSendString("play shootmusic ",NULL,0,NULL);
		}
		  
	}*/
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		clean();  // 把之前绘制的内容清除
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
		show();  // 显示新画面
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}